/*
 * @Author       : NaN 353362604@qq.com
 * @Date         : 2023-05-18 09:59:00
 * @LastEditTime : 2023-06-27 10:31:19
 * @LastEditors  : NaN 353362604@qq.com
 * @FilePath     : \AdminStration\src\Common\Router\MenuNav\module\SelectModal.jsx
 * @Description  :
 */
import { Http } from '../../../Api'
import { useRequest } from 'ahooks'
import request from 'umi-request'
import { useState, useEffect, useRef } from 'react'
import { ProTable } from '@ant-design/pro-components'
import { Button } from 'antd'

export const SelectModal = ({ height, TableListData, router }) => {
    const actionRef = useRef()
    const headers = { Authorization: sessionStorage.getItem('TOKEN') }
    const [paramsData, setParamsData] = useState(1)
    const [SizeNumber, setSizeNumber] = useState(10)
    const [TableListParament, setTableListParament] = useState()

    useEffect(() => {
        const { page, ...res } = TableListData
        const ClientData = JSON.parse(localStorage.getItem('ClientData'))
        const getProList = async () => {
            const Template = await import(`../${router}/${page}`).catch(() => false)
            setTableListParament(await Template[page]({ ...res, ClientData }))
        }
        if (page) getProList()
    }, [TableListData, router])
    const { runAsync } = useRequest(Http, {
        manual: true,
        onSuccess: () => {
            TableListData.actionRef.current.reload()
            actionRef.current.reload()
        },
    })
    const Submit = data => {
        runAsync({
            url: TableListParament.pos,
            data: TableListParament.assemblyData(data),
            path: [TableListParament.uuid],
        })
    }

    return (
        <>
            {TableListParament && (
                <ProTable
                    actionRef={actionRef}
                    columns={TableListParament.columns}
                    params={TableListParament.params}
                    request={async (params = {}) => {
                        delete params.current
                        params.pageIndex = paramsData || 1
                        return request(TableListParament.get, { params, headers }).then(res => {
                            return { data: res.list, total: res.total, success: true }
                        })
                    }}
                    pagination={{
                        pageSize: SizeNumber,
                        showSizeChanger: true,
                        onChange: (e, a) => {
                            setParamsData(e)
                            setSizeNumber(a)
                        },
                    }}
                    columnsState={{ persistenceKey: 'pro-table-singe-demos', persistenceType: 'localStorage' }}
                    rowKey="uuid"
                    toolBarRender={() => TableListParament.options}
                    search={TableListParament.hideInSearch ? false : { span: 12 }}
                    scroll={{ y: router === 'TwoRouter' ? height - 168 : height - 320 }}
                    rowSelection={
                        TableListParament.hideInSelect
                            ? false
                            : { type: TableListParament?.isCheckbox ? 'checkbox' : 'radio' }
                    }
                    tableAlertOptionRender={({ selectedRows }) => (
                        <Button type="primary" onClick={() => Submit(selectedRows)}>
                            保存
                        </Button>
                    )}
                />
            )}
        </>
    )
}
